//
//  RegisterView.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/12.
//

import SwiftUI
import FlowStacks

struct RegisterView: View {
    @StateObject private var viewModel = RegisterViewModel()
    @State var showAgreementAlert = false
    @EnvironmentObject var navigator: FlowNavigator<LoginStack>
    
    let leftOffset = 24.0

    
    var body: some View {
        ZStack{
            VStack(alignment: .leading) {
            NavigationBar(title: "")
            HStack{
                Text("注册")
                    .font(.system(size: 24))
                    .foregroundColor(AppColors.Gy1)
                    .bold()
                Spacer()
            }.padding(.leading, 24)
                .padding(.top, 20)
            
            VStack {
                // 账号输入框
                CustomTextField(
                    text: $viewModel.account,
                    placeholder: "email or phoneNum",
                    isValid: viewModel.account.isEmpty || viewModel.account.isEmail || viewModel.account.isPhoneNum
                )
                .disabled(viewModel.isLoading)
            }
            .padding(.horizontal, leftOffset)
            .padding(.top, 48)
            AgreeView(isAgree: $viewModel.isAgreed)
               .padding(.leading, leftOffset)
                .padding(.top, 15)
            
            HStack {
                Button(action: {
                    if viewModel.isAgreed {
                        viewModel.requestCode()
                    }
                    else{
                        showAgreementAlert = true
                    }
                }) {
                    ZStack {
                        if viewModel.isLoading {
                            ProgressView()
                                .progressViewStyle(CircularProgressViewStyle(tint: .white))
                        } else {
                            Text("login")
                                .font(.system(size: 16, weight: .medium))
                        }
                    }
                    .frame(maxWidth: .infinity)
                    .frame(height: 44)
                    .background(viewModel.isAccountValid && !viewModel.isLoading ? AppColors.Brand6 : AppColors.Brand6.opacity(0.5))
                    .cornerRadius(6)
                    .foregroundColor(.white)
                    .contentShape(Rectangle())
                }
                .disabled(viewModel.isLoading || !viewModel.isAccountValid)
                .padding(.horizontal, leftOffset)
            }.padding(.top, 25)
            
            Spacer()
            
        }
        
        if showAgreementAlert == true{
            AgreementPopupOverlay(
                onTapUserAgreement: { print("用户协议点击") },
                onTapPrivacyPolicy: { print("隐私政策点击") },
                onDecline: { viewModel.isAgreed = false ; showAgreementAlert = false },
                onConfirm: {
                    viewModel.isAgreed = true
                    showAgreementAlert = false
                }
            )
        }
    }.onChange(of: viewModel.sendSuccess) { success in
            if success {
                navigator.push(.verificationCode(account: viewModel.account, type: .register, verifyType: .register))
            }
        }
    .onAppear{
        viewModel.resetStatus()
    }

    }
}
